<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分店采购明细表</title>
<link rel="stylesheet" href="../../res/mln/layui.css" media="all">
<style type="text/css">

#anniu{
    	position: relative;
    	left:655px;
    	top:-30px;
    
    }
</style>
</head>
<body>

	  <table class="layui-hide" id="fdPoBreakdown" lay-filter="demo"></table>
	
	<!-- 文本域 -->
	<div class="text">
		<textarea id="textarea" rows="4" cols="122" placeholder="请输入退货原因"  maxlength="10" style="border:1px solid #dddddd" required></textarea>
	</div>
	
	</div>

<script type="text/html" id="barDemo">

  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="salesReturn">退货</a>
</script>



<script type="text/html" id="salesReturnQuantity">
	<div class="layui-inline">
	    <input class="layui-input input" name="{{ d.fdPodId}}" id="demoReload" autocomplete="off" style="height:30px" onkeyup="num(this);" maxlength="5" required >
		<input class="layui-input" type="hidden" id="{{ d.fdPodId}}">
	 </div>
</script>

<script src="../../res/mln/jquery-1.10.2.js"></script>
<script src="../../res/mln/layui-xtree.js"></script>
<script src="../../res/mln/layui.js"></script>
<script src="../../res/js/base.js"></script>
<script type="text/javascript" src="../../res/mln/getUrlParam.js"></script>


<script>
var str = "";
layui.use(['form','laydate', 'laypage', 'layer', 'table', 'upload', 'element','tree'], function(){
  var form = layui.form
  ,laydate = layui.laydate //日期
  ,laypage = layui.laypage //分页
  ,layer = layui.layer //弹层
  ,table = layui.table //表格
  ,upload = layui.upload //上传
  ,element = layui.element; //元素操作
  var path=getRootPath();
  
  
  //取到采购订单ID
  var fdPoId = getUrlParam('fdPoId');
 
  
  //上传文件
  var uploadInst = upload.render({
      elem: '#upload1' //绑定元素
      ,url: path+'member/uploadFile.action' //上传接口
      ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
        	  
              $('#demoOne').attr('src', result); //图片链接（base64）
          });
      }
      ,done: function(res){
          //如果上传失败
          if(res.code > 0){
              return layer.msg('上传失败');
          }
          //上传成功
          document.getElementById("img_url").value = res.url;

      }
      ,error: function(){
          //演示失败状态，并实现重传
          var demoText = $('#demoText');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
          demoText.find('.demo-reload').on('click', function(){
              uploadInst.upload();
          });
      }
  });
  //上传文件
  var uploadInsts = upload.render({
      elem: '#uploadTwo' //绑定元素
      ,url: path+'member/uploadFile.action' //上传接口
      ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
        	  
              $('#demoTow').attr('src', result); //图片链接（base64）
          });
      }
      ,done: function(res){
          //如果上传失败
          if(res.code > 0){
              return layer.msg('上传失败');
          }
          //上传成功
          document.getElementById("img_urlTwo").value = res.url;

      }
      ,error: function(){
          //演示失败状态，并实现重传
          var demoText = $('#demoTextTwo');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
          demoText.find('.demo-reload').on('click', function(){
        	  uploadInsts.upload();
          });
      }
  });
  
  //监听Tab切换
  element.on('tab(demo)', function(data){
    layer.msg('切换了：'+ this.innerHTML);
    console.log(data);
  });
  
  //执行一个 table 实例
  table.render({
     elem: '#fdPoBreakdown'
    ,async: false
  
    ,url: path+'FdPoBreakdown/findAll.action?fdPoId='+fdPoId //数据接口
    ,page: true //开启分页
    ,method:'post'
    ,size:'sm'
    ,cols: [[ //表头
       /* {field: 'fdPodId', title: '采购明细ID', width:100 }
      ,{field: 'fdPoId', title: '采购订单ID', width:100} */
       {field: 'drugName', title: '药品名称', width:150}
      ,{field: 'fdItemNumber', title: '物品数量', width:100}
      ,{field: 'fdSubtotal', title: '小计', width:100}
      ,{field: 'fdItemRemark', title: '物品备注', width:120}
      ,{field: 'fdUnit', title: '物品单位', width:100}
      ,{field: 'fdPrice', title: '进价', width:100}
      ,{field: 'ythNumber', title: '已退货数量', width:120}
      ,{field: 'salesReturnQuantity', title: '退货数量', width:120,templet:'#salesReturnQuantity'}

     
      
    ]]
  	,id: 'testReload'
	,done: function(res, curr, count){
  		str = table.cache.testReload;
  	  }
  });
  
  //监听工具条
  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
    var data = obj.data //获得当前行数据
    ,layEvent = obj.event; //获得 lay-event 对应的值
    if(layEvent === 'del'){
      var reId=data.reId;
      layer.confirm('确认删除？', function(index){
        obj.del(); //删除对应行（tr）的DOM结构
        layer.close(index);
        //使用jquery的ajax方式向后台提交数据
		var url = path+"salesReturn/deleteReId.action";
		var data={reId:reId};
			$.post(url,data,function(data){
				
				if(data=="ok"){
					layer.msg('删除成功', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						  
					});
				}
			});
      	});
    } else if(layEvent === 'edit'){
	      
	      $("#memberName").val(data.memberName);
	      
	      $("#memberNumber").val(data.memberNumber);
	      $("#menmberPwd").val(data.menmberPwd);
	      $("#img_urlTwo").val(data.memberImage);
	      $("#demoTow").attr("src",path+data.memberImage); 
	      $("#memberId").val(data.memberId);
	      
	      var m=$("#isva").children().next();
	      var n=$("#isva").children();
	      
	      if(m.val()==data.memberSort){      
	         m.attr("selected",true);
	      }else{
	    	 n.attr("selected",true);
	      }
	      form.render("select");
	      $('#updateReload').css('display','block');
		}
    else if(layEvent === 'salesReturn'){
		var fdPoId = data.fdPoId;
		layer.open({
			title:'分店采购明细表'
			,type:2
			,area:['85%','80%']
			//设置滚蛋条 yes为开启  no为关闭
			,content:[path+'pages/mln/fdSellDetail.html?sellId='+sellId,'yes']
		});
 
    }
    });

		//条件查询
		var $ = layui.$;

		$('.demoTable .layui-btn').on('click', function() {
			var demoReload = $('#demoReload');
			//执行重载
			table.reload('testReload', {
				page : {
					curr : 1
				//重新从第 1页开始
				},
				where : {
					reCause : demoReload.val()
				}
			});
		});
		
		
		//条件查询
		/* var $ = layui.$; */
	
		
		
		//点击提交财务，库存做处理
		$('#anniu').on('click',function(){
			if($(".input").val() == ""){
				layer.msg("必填项不能为空");
				return;
			}
			//取到退货原因
			var reCause = $("#textarea").val();
			//取到退货总金额
			var totalMoney = $("#totalMoney").text();
			var pfxqSubtotal  = data
			
			var arr = [];
			var strs = "";
			for(var i=0;i<str.length;i++){
				var rows = "";
				if(i<(str.length-1)){
					rows = {"fdPodId":str[i].fdPodId,"rdNumber":$("#"+str[i].fdPodId).val(),"drugId":str[i].drugId,"rdSubtotal":str[i].fdSubtotal,"fdItemNumber":str[i].fdItemNumber};
				}else {
					rows = {"fdPodId":str[i].fdPodId,"rdNumber":$("#"+str[i].fdPodId).val(),"drugId":str[i].drugId,"rdSubtotal":str[i].fdSubtotal,"fdItemNumber":str[i].fdItemNumber};
				}
				arr.push(rows);
			}
			var jsonStr = JSON.stringify(arr);
			var url = path+"detail/xgSlCg.action";
		    var data={"jsonStr":jsonStr,"reCause":reCause,"totalMoney":totalMoney};
			$.post(url,data,function(data){
				alert(data);
				if(data=="ok"){
					layer.msg('退货成功', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
							parent.location.reload();
						  
					});
				}
				else{
					layer.msg('退货失败', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						location.href='fdSellDetail.html';
					});
				}
			});
		})
		
		$('.demoTable .layui-btn').on('click', function() {
			var demoReload = $('#demoReload');
			//执行重载
			table.reload('testReload', {
				page : {
					curr : 1
				//重新从第 1页开始
				},
				where : {
					reCause : demoReload.val()
				}
			});
		});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		$('#closeAdd').on('click', function() {
			$('#addReload').css('display', 'none');
		});
		$('#closeUpdate').on('click', function() {
			
			$('#updateReload').css('display', 'none');
		});
		
		$('#memberAdd').on('click', function() {
			$('#addReload').css('display', 'block');
			
		});
	});
	
	
//限制只能输入数字的正则表达式
function num(obj){
	obj.value = obj.value.replace(/[^\d]/g,"");
	$("#totalMoney").text("0");
	$("#totalNumber").text("0");
	var totalMoney = 0;
	var totalNumber = 0;
	for(var i=0;i<str.length;i++){
		if($(obj).attr("name") == str[i].fdPodId){
			if(obj.value > str[i].fdItemNumber){
				obj.value = str[i].fdItemNumber;
			}
		}
		$("#"+$(obj).attr("name")).val(obj.value);
		//alert(str[i].mxQuantity+obj.value);
		var numOne = $("#"+str[i].fdPodId).val()-0;
		var numTwo = str[i].fdItemNumber-0;
		//如果退货数量小于总数量
		if(numOne <= numTwo){
			totalMoney += str[i].fdPrice * numOne;
			totalNumber += numOne;
		}else{
			//判断药品名称是否等于此ID
			if($(obj).attr("name") == str[i].fdPodId){
				obj.value = str[i].fdItemNumber;
			}
			totalMoney += str[i].fdPrice * numTwo;
			totalNumber += str[i].fdItemNumber;
		}
	};
	$("#totalMoney").text(totalMoney);
	$("#totalNumber").text(totalNumber);
}

</script>
<div><h3 style="color:#c2c2c2">总计:<span id="totalMoney">0</span>元</h3><h3 style="color:#c2c2c2" >总数量:<span id="totalNumber">0</span>件</h3><button class="layui-btn layui-btn-radius" id="anniu">立即提交</button></div>
</body>
</html>